package com.litxuf.logic.member.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.litxuf.logic.member.dao.model.School;

public class MemberDAO extends HibernateDaoSupport  {

	public List findAllMember(final int curpage, final int pagesize,
			String sorlrule) {
		StringBuffer sb = new StringBuffer("from School t ");


		final String hqlStr = sb.toString();
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery(hqlStr).setFirstResult(
						(curpage - 1) * pagesize).setMaxResults(pagesize)
						.list();
			}
		});

	}

	public int findToatlMember() {
		StringBuffer sb = new StringBuffer(
				"SELECT COUNT(*) FROM School ");

		List list = this.getHibernateTemplate().find(sb.toString());
		Long num = (Long) list.get(0);
		return num.intValue();
	}

	 

	 
	/**
	 * 用户名查询
	 * made in 2010-12-5下午12:52:32
	 * @param memberName
	 * @return
	 *
	 */
 
	public School findByMemberName(String memberName) {

		String sql = "from School t where t.memberName='" + memberName
				+ "'";

		List ls = this.getHibernateTemplate().find(sql);
		if (ls != null && ls.size() > 0)
			return (School) ls.get(0);
		else
			return null;
	}

	/**
	 * 用户名和密码查询
	 * made in 2010-12-5下午12:52:18
	 * @param name
	 * @param pwd
	 * @return
	 *
	 */
	public School findByMemberNameAndPwd(String name, String pwd) {
		String sql = "from School t where t.memberName='" + name
				+ "' and t.memberPwd='" + pwd + "'";

		List ls = this.getHibernateTemplate().find(sql);
		if (ls != null && ls.size() > 0)
			return (School) ls.get(0);
		else
			return null;
	}

	public String saveMember(School member) {
		String pk = null;
		try {
			pk = getHibernateTemplate().save(member).toString();
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		}
		return pk;
	}

	public void updateMember(School member) {

		this.getHibernateTemplate().update(member);

	}

	public boolean updatePwdByMembername(String username) {
		School m = this.findByMemberName(username);

		if (m != null) {
			m.setMemberPwd("000000");
			this.updateMember(m);
			return true;
		}
		return false;
	}

	 

	 

	public School findMemberByMemberId(int memberId) {
		return (School) this.getHibernateTemplate().get(
				School.class, memberId);
	}

	public School findByMemberNameAndMail(String name, String email) {
		String sql = "from School t where t.memberName='" + name
				+ "' and t.email='" + email + "'";

		List ls = this.getHibernateTemplate().find(sql);
		if (ls != null && ls.size() > 0)
			return (School) ls.get(0);
		else
			return null;
	}

	 

	
	
	
	/**
	 * 后台多条件查询
	 * made in 2010-12-5下午12:46:49
	 * @param schoolname
	 * @param schoolType
	 * @param schoolXingzhi
	 * @param curpage
	 * @param pagesize
	 * @return
	 *
	 */
	public List findSchool4Admin(String schoolname,String schoolType,String schoolXingzhi,String areacode,final int curpage, final int pagesize){
		
		final String hqlStr = genSQL(schoolname, schoolType, schoolXingzhi,areacode, 1);
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return session.createQuery(hqlStr).setFirstResult(
						(curpage - 1) * pagesize).setMaxResults(pagesize)
						.list();
			}
		});
	}
	
	/**
	 * 后台多条件查询
	 * made in 2010-12-5下午12:47:03
	 * @param schoolname
	 * @param schoolType
	 * @param schoolXingzhi
	 * @return
	 *
	 */
	public int findSchool4Admin(String schoolname,String schoolType,String schoolXingzhi,String areacode) {
		  String hqlStr = genSQL(schoolname, schoolType, schoolXingzhi,areacode, 0);
		List list = this.getHibernateTemplate().find(hqlStr);
		Long num = (Long) list.get(0);
		return num.intValue();
	}
	
	public String genSQL(String schoolname,String schoolType,String schoolXingzhi,String areacode, int flag) {
		StringBuffer sb = null;
		if (flag == 1) {
			sb = new StringBuffer("from School t where 1=1 ");
		} else {
			sb = new StringBuffer("select count(*) from School t where 1=1 ");
		}

		if (StringUtils.isNotBlank(schoolname)) {
			sb.append(" and  t.schoolname like '%" + schoolname + "%'");
		}

		if (StringUtils.isNotBlank(schoolType)) {
			sb.append(" and  t.schoolType='" + schoolType + "'");
		}
		if (StringUtils.isNotBlank(schoolXingzhi)) {
			sb.append(" and  t.schoolXingzhi ='"+schoolXingzhi+"'");
		}
		 
		if (StringUtils.isNotBlank(areacode)) {
			sb.append(" and  t.areaCodeProvince  = '"+areacode+"'");
		}
		if(flag == 1){
			sb.append(" order by t.regdatetime desc");
			
		}
		return sb.toString();

	}
}
